package com.sesame.controller;

import com.sesame.api.FMSConfig;
import kim.sesame.framework.entity.GMap;
import kim.sesame.framework.utils.Argument;
import kim.sesame.framework.utils.DateUtils;
import kim.sesame.framework.utils.ImgBase64;
import kim.sesame.framework.web.controller.AbstractWebController;
import kim.sesame.framework.web.response.Response;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

/**
 * @author johnny
 * @date 2017-06-10 13:10
 * @Description:
 */
@CommonsLog
@Controller
@RequestMapping("/index")
public class IndexController extends AbstractWebController {

    @Autowired
    private FMSConfig fmsConfig;

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/upload")
    @ResponseBody
    private Response upload(String file, String projectName, String moduleName, String fileName) throws Exception {
        Argument.notEmpty(file, "文件base64字符串 ( file ) 不能为空");
        Argument.notEmpty(projectName, "项目名称 (projectName) 不能为空");
        Argument.notEmpty(moduleName, "模块名称 (moduleName) 不能为空");
        Argument.notEmpty(fileName, "文件名称 (fileName) 不能为空");
        Date d = new Date();
        String filePath = "/" + projectName + "/" + moduleName + "/" +
                DateUtils.convert(d, "yyyy/MM/dd");

        checkAndCreate(fmsConfig.getFilePath() + filePath);

        filePath = filePath + "/" + fileName;
        String toFilePath = fmsConfig.getFilePath() + filePath;

        byte[] b = ImgBase64.imageConversionByte(file);
        OutputStream out = new FileOutputStream(toFilePath);
        out.write(b);
        out.flush();
        out.close();

        GMap map = GMap.newMap();
        map.putAction("path", filePath);
        map.putAction("service", fmsConfig.getMapping());
        map.putAction("src", fmsConfig.getMapping() + filePath);

        return returnSuccess(map);
    }

    @RequestMapping("/upload_file")
    @ResponseBody
    private Response upload_file(String projectName, String moduleName, MultipartFile file) throws Exception {

        Argument.notEmpty(projectName, "项目名称 (projectName) 不能为空");
        Argument.notEmpty(moduleName, "模块名称 (moduleName) 不能为空");

        String filePath = "'";
        if (file.getSize() > 0) {
            Date d = new Date();
            filePath = "/" + projectName + "/" + moduleName + "/" +
                    DateUtils.convert(d, "yyyy/MM/dd");

            checkAndCreate(fmsConfig.getFilePath() + filePath);

            filePath += "/" + d.getTime() + "_" + file.getOriginalFilename();

            String targetFilePath = fmsConfig.getFilePath() + "/" + filePath;

            //            file.transferTo(targetFile);

            saveFile1(targetFilePath, file);
//            saveFile2(targetFilePath, file);
        }

         GMap map = GMap.newMap();
        map.putAction("path", filePath);
        map.putAction("service", fmsConfig.getMapping());
        map.putAction("src", fmsConfig.getMapping() + filePath);

        return returnSuccess(map);
    }

    public void saveFile1(String targetFilePath, MultipartFile file) throws Exception {
        File targetFile = new File(targetFilePath);
        OutputStream outputstream = null;
        InputStream inputstream = null;
        try {
            outputstream = new FileOutputStream(targetFile);
            inputstream = file.getInputStream();
            FileCopyUtils.copy(inputstream, outputstream);
        } finally {
            if (outputstream != null) {
                outputstream.flush();
                outputstream.close();
            }
            if (inputstream != null) {
                inputstream.close();
            }
        }


    }

    public static boolean saveFile2(String localUrl, MultipartFile file) throws IOException {
        File realFile = new File(localUrl);
        OutputStream outputstream = null;
        InputStream inputstream = null;
        try {
            inputstream = file.getInputStream();
            outputstream = new FileOutputStream(realFile);
            byte[] buffer = new byte[1024 * 5];
            int byteRead = -1;
            while ((byteRead = (inputstream.read(buffer))) != -1) {
                outputstream.write(buffer, 0, byteRead);
            }
            realFile.setExecutable(false);
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            if (outputstream != null) {
                outputstream.flush();
                outputstream.close();
            }
            if (inputstream != null) {
                inputstream.close();
            }
        }
    }


    public void checkAndCreate(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
}